package com.xpgk.mvc.user;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.log.Log;
import com.xpgk.annotation.Table;
import com.xpgk.mvc.base.BaseModel;
import com.xpgk.mvc.dept.Department;
import com.xpgk.mvc.deptStation.DeptStation;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.plugin.ParamInitPlugin;
import com.xpgk.tools.ToolCache;

/**
 * 用户model
 * 
 */
@SuppressWarnings("unused")
@Table(tableName = User.table_name)
public class User extends BaseModel<User> {

	private static final long serialVersionUID = 6761767368352810428L;

	private static final Log log = Log.getLog(User.class);

	public static final User dao = new User().dao();

	/**
	 * 表名称
	 */
	public static final String table_name = "SYS_USER";


	
	/**
	 * 字段描述：主键 字段类型：character varying 长度：32
	 */
	public static final String column_ids = "IDS";

	/**
	 * 字段描述：账号 字段类型：character varying 长度：50
	 */
	public static final String column_username = "USERNAME";

	/**
	 * 字段描述：密码 字段类型：character varying 长度：500
	 */
	public static final String column_password = "PASSWORD";

	/**
	 * 字段描述：密钥 字段类型：character varying 长度：500
	 */
	public static final String column_salt = "SALT";

	/**
	 * 字段描述：账号状态 字段类型：character 长度：1
	 */
	public static final String column_status = "STATUS";

	/**
	 * 字段描述：名称 字段类型：character varying 长度：25
	 */
	public static final String column_name = "NAME";

	/**
	 * 字段描述：所在部门ids 字段类型：character varying 长度：32
	 */
	public static final String column_departmentids = "DEPARTMENTIDS";

	/**
	 * 字段描述：所在岗位ids 字段类型：character varying 长度：32
	 */
	public static final String column_stationids = "STATIONIDS";

	/**
	 * 字段描述：所在机构ids 字段类型：character varying 长度：32
	 */
	public static final String column_organizationids = "ORGANIZATIONIDS";

	/**
	 * 字段描述：身份证号 字段类型：character varying 长度：25
	 */
	public static final String column_idcard = "IDCARD";

	/**
	 * 字段描述：手机号 字段类型：character varying 长度：20
	 */
	public static final String column_mobile = "MOBILE";

	/**
	 * 字段描述：邮箱 字段类型：character varying 长度：100
	 */
	public static final String column_email = "EMAIL";

	/**
	 * 字段描述：idea加密key 字段类型：character varying 长度：100
	 */
	public static final String column_secretkey = "SECRETKEY";
	/**
	 * 字段描述：2位验证码
	 */
	public static final String column_verificationCode = "VERIFICATIONCODE";
	
	
	/**
	 * 字段描述：该条数据添加人员is  字段类型：character varying 长度：32
	 */
	public static final String column_createId = "CREATEID";
	/**
	 * 字段描述：该条数据添加时间  字段类型：datetime 长度：0
	 */
	public static final String column_createDate = "CREATEDATE";
	/**
	 * 字段描述：该条数据更新人员id  字段类型：character varying 长度：100
	 */
	public static final String column_updateId = "UPDATEID";
	/**
	 * 字段描述：该条数据更新时间  字段类型：datetime 长度：0
	 */
	public static final String column_updateDate = "UPDATEDATE";
	/**
	 * sqlId : 获取指定部门下说有人员
	 */
	public static final String sqlId_findUserByDept = "sys.user.findUserByDept";

	/**
	 * sqlId : sys.user.userList 描述：用户list
	 */
	public static final String sqlId_userList = "sys.user.userList";

	/**
	 * sqlId : sys.user.organUserList 描述：用户list
	 */
	public static final String sqlId_organUserList = "sys.user.organUserList";

	/**
	 * sqlId : sys.user.splitPageSelect 描述：分页Select
	 */
	public static final String sqlId_splitPageSelect = "sys.user.splitPageSelect";

	/**
	 * sqlId : sys.user.splitPageFrom 描述：分页from
	 */
	public static final String sqlId_splitPageFrom = "sys.user.splitPageFrom";
	
	/**
	 * sqlId : sys.user.splitPageFrom 描述：分页from
	 */
	public static final String sqlId_splitPageFrom2 = "sys.user.splitPageFrom2";

	/**
	 * sqlId : sys.user.paging 描述：查询所有用户
	 */
	public static final String sqlId_paging = "sys.user.paging";

	/**
	 * sqlId : sys.user.treeUserNode 描述：根据部门Id查询，人员树节点数据
	 */
	public static final String sqlId_treeUserNode = "sys.user.treeUserNode";
	/**
	 * sqlId : sys.user.treeUserNode 描述：根据 机构Id查询人员数据
	 */
	public static final String sqlId_userListByorgId = "sys.user.userListByorgId";
	/**
	 * sqlId : sys.user.treeUserNode 描述：根据 机构Id查询人员数据
	 */
	public static final String sqlId_userListByorgIdTwo = "sys.user.userListByorgIdTwo";

	/**
	 * sqlId : sys.user.column 描述：查询用户，自定义字段和值
	 */
	public static final String sqlId_column = "sys.user.column";

	/**
	 * sqlId : sys.user.stop 描述：停用账户
	 */
	public static final String sqlId_stop = "sys.user.stop";

	/**
	 * sqlId : sys.user.start 描述：启用停用账户
	 */
	public static final String sqlId_start = "sys.user.start";

	/**
	 * 关联查询，获取用户详细信息
	 * 
	 * @return
	 */
	public UserInfo getUserInfo() {
		return UserInfo.dao.findById(this.getPKValue());
	}

	/**
	 * 关联查询，获取用户部门信息
	 * 
	 * @return
	 */
	public Department getDepartment() {
		return Department.dao.findById(this.getStr(column_departmentids));
	}

	/**
	 * 关联查询，获取用户岗位信息
	 * 
	 * @return
	 */
	public DeptStation getStation() {
		return DeptStation.dao.findById(this.getStr(column_stationids));
	}

	/**
	 * 关联查询，获取用户机构信息
	 * 
	 * @return
	 */
	public Organization getOrganization() {
		return Organization.dao.findById(this.getStr(column_organizationids));
	}

	/**
	 * 添加或者更新缓存
	 */
	public static void cacheAdd(String ids) {
		User user = User.dao.findById(ids);
		if (user != null) {
			ToolCache.set(ParamInitPlugin.cacheStart_user + ids, user);
			ToolCache.set(ParamInitPlugin.cacheStart_user + user.getStr(column_username), user);
			ToolCache.set(ParamInitPlugin.cacheStart_user + user.getStr(column_mobile), user);
			ToolCache.set(ParamInitPlugin.cacheStart_user + user.getStr(column_email), user);
			ToolCache.set(ParamInitPlugin.cacheStart_user + user.getStr(column_idcard), user);
		}
	}

	/**
	 * 删除缓存
	 */
	public static void cacheRemove(String ids) {
		User user = User.dao.findById(ids);
		if (user != null) {
			ToolCache.remove(ParamInitPlugin.cacheStart_user + ids);
			ToolCache.remove(ParamInitPlugin.cacheStart_user + user.getStr(column_username));
			ToolCache.remove(ParamInitPlugin.cacheStart_user + user.getStr(column_mobile));
			ToolCache.remove(ParamInitPlugin.cacheStart_user + user.getStr(column_email));
			ToolCache.remove(ParamInitPlugin.cacheStart_user + user.getStr(column_idcard));
		}
	}

	/**
	 * 获取缓存
	 * 
	 * @param ids
	 * @return
	 */
	public static User cacheGetByUserId(String ids) {
		User user = ToolCache.get(ParamInitPlugin.cacheStart_user + ids);
		if (user == null) {
			user = User.dao.findById(ids);
			if (user != null) {
				cacheAdd(ids);
			}
		}
		return user;
	}

	/**
	 * 获取缓存
	 * 
	 * @param userName
	 * @return
	 */
	public static User cacheGetByUserName(String userName) {
		if (StringUtils.isNotBlank(userName)) {
			//User user = ToolCache.get(ParamInitPlugin.cacheStart_user + userName);
			///User user = 
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", column_username);
			String sql = getSqlByBeetl("sys.user.column", param);
			User user = User.dao.findFirst(sql, userName);
			
				
		
			//if (user == null) {
//				Map<String, Object> param = new HashMap<String, Object>();
//				param.put("column", column_username);
//				String sql = getSqlByBeetl("sys.user.column", param);
//				user = User.dao.findFirst(sql, userName);
				if (user != null) {
					cacheAdd(user.getPKValue());
				}else{
					Map<String, Object> param2 = new HashMap<String, Object>();
					param2.put("column", column_mobile);
					String sql2 = getSqlByBeetl("sys.user.column", param2);
					User user2 = User.dao.findFirst(sql2, userName);
					if (user2 != null) {
						user = user2;
						cacheAdd(user.getPKValue());
					}
				}
		//	}
			return user;
		} else {
			return null;
		}
	}

	/**
	 * 获取缓存
	 * 
	 * @param mobile
	 * @return
	 */
	public static User cacheGetByMobile(String mobile) {
		User user = ToolCache.get(ParamInitPlugin.cacheStart_user + mobile);
		if (user == null) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", column_mobile);
			String sql = getSqlByBeetl("platform.user.column", param);
			user = User.dao.findFirst(sql, mobile);
			if (user != null) {
				cacheAdd(user.getPKValue());
			}
		}
		return user;
	}

	/**
	 * 获取缓存
	 * 
	 * @param email
	 * @return
	 */
	public static User cacheGetByEmail(String email) {
		User user = ToolCache.get(ParamInitPlugin.cacheStart_user + email);
		if (user == null) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", column_email);
			String sql = getSqlByBeetl("platform.user.column", param);
			user = User.dao.findFirst(sql, email);
			if (user != null) {
				cacheAdd(user.getPKValue());
			}
		}
		return user;
	}

	/**
	 * 获取缓存
	 * 
	 * @param idcard
	 * @return
	 */
	public static User cacheGetByIdcard(String idcard) {
		User user = ToolCache.get(ParamInitPlugin.cacheStart_user + idcard);
		if (user == null) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", column_idcard);
			String sql = getSqlByBeetl("platform.user.column", param);
			user = User.dao.findFirst(sql, idcard);
			if (user != null) {
				cacheAdd(user.getPKValue());
			}
		}
		return user;
	}

}
